#include <iostream>
#include <memory>
#include <string>
#include "Protocol.hpp"
#include "InetAddr.hpp"
#include "Common.hpp"

void Init(int &x, int &y, char &oper)
{
    std::cout << "Enter Please x: ";
    std::cin >> x;
    std::cout << "Enter Please y: ";
    std::cin >> y;
    std::cout << "Enter Please oper: ";
    std::cin >> oper;
}
void USER(const char *buf)
{
    std::cerr << buf << " ip port" << std::endl;
}
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        USER(argv[0]);
        exit(USAGE_ERR);
    }

    uint16_t port = std::stoi(argv[2]);
    std::string ip = argv[1];

    Inet_Addr addr(ip, port);

    std::shared_ptr<Socket> client = std::make_shared<TcpSocket>();
    client->BuildTcpClientSocketMethod();

    std::unique_ptr<Protocol> pro = std::make_unique<Protocol>();

    // 建立连接
    if (client->Connect(addr))
    {
        std::cerr << "connect error" << std::endl;
        exit(CONNECT_ERR);
    }
    std::string buffer;
    while (true)
    {
        std::string send;
        // 获取输入信息
        int x, y;
        char oper;
        //int cnt = 3;
        //while (cnt--)
        //{
            Init(x, y, oper);
            Request req(x, y, oper);
            send += pro->BuildRequestSring(req);
        //}
        // std::cout << "---------client_send_string-------------" << std::endl;
        // std::cout << send << std::endl;
        // std::cout << "----------------------------------------" << std::endl;
        // send += "50";
        // 将输入的信息发送给服务器
        client->Send(send);

        // 接收返回的消息
        Response relust;
        pro->GetRespones(client, relust, buffer);

        // relust.LestRelust();
    }

    return 0;
}